import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
import axios from 'axios'
import { MessageBox } from 'element-ui';
import { Message } from 'element-ui';
export default new Vuex.Store({
  state: {
    list: []
  },
  mutations: {  
    SET_LIST(state, payload) {
      state.list = payload
    },
    DELETE_LIST(state, id) {
      MessageBox.confirm('确认要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        state.list = state.list.filter(item=> item.id !==id )

        Message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        Message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    },
    MODIFY_LIST(state, payload) {
      let o = state.list.find(item => item.id === payload.id)
      Object.assign(o, payload)

      // o.name = payload.name
      // o.title = payload.title
      // o.id = payload.id
      // o.datetime = payload.datetime
      // o.address = payload.address
    },
    ADD_LIST(state, payload) {
      state.list.push(payload)
      console.log(state.list);
    }
  },
  actions: {
    async GET_LIST({ commit }) {
      const resp =  await axios.get('/api/list')
      commit('SET_LIST', resp.data.data)
    }
  },
  modules: {
  }
})
